package wl.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import wl.domain.EnterprisePojo;
import wl.service.EnterprisePojoService;

@Controller
@RequestMapping(value="/enterprise")
public class EnterpriseController {

	@Autowired
	EnterprisePojoService enterprisePojoService;
	
	//其实在数据库里没必要保存传路径，这个路径在显示的时候拼接上就行，数据库只保存文件名就好了
	String uploadPath="c:\\a\\";
	
	@RequestMapping(value="/queryAll")	
	public String query(Model model ,
			@RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
			Integer pageSize){
		System.out.println(pageNum);
		
		pageSize=3;
		List<EnterprisePojo> enterprisePojos=enterprisePojoService.queryAll(pageNum, pageSize);	
		for(EnterprisePojo ep:enterprisePojos){
			System.out.println(ep.getEnte_name());
		}
		model.addAttribute("enterprisePojos", enterprisePojos);
		return "enterpriseList";
	}

	@RequestMapping(value="/insert")
	public void insert(EnterprisePojo enterprisePojo,
			@RequestParam("licence") MultipartFile licence, 
			@RequestParam("img_lb") List<MultipartFile> files ){
		
		if(!licence.isEmpty()){
		//将单个文件传递给文件上传处理器（自己写），返回文件的存储位置
		String business_licence=this.singleFileUploadHandler(licence);
		//将文件的存储路径保存到企业对象中
		enterprisePojo.setBusiness_licence(business_licence);
		}
		
		if(!files.isEmpty()){
			//处理多文件上传
			String ente_img_lb=this.listFileUploadHandler(files);
			enterprisePojo.setEnte_img_lb(ente_img_lb);	
			
		}
		
		enterprisePojoService.insertEnterprisePojo(enterprisePojo);
	
	}
	
	
	public String singleFileUploadHandler(MultipartFile file){
		
		        //生成唯一文件名
				String id = UUID.randomUUID().toString();
				
				//将uuid与原始文件名，拼为唯一图片名
				String pictureUUIDName=id+file.getOriginalFilename();
				
				//利用上传路径和唯一图片名合成为File的url
				
				String fileUrl=uploadPath+pictureUUIDName;
				
				try {
					//采用commons.io 提供的文件工具，将文件写出到指定路径
					FileUtils.writeByteArrayToFile(new File(fileUrl),file.getBytes());//双反斜杠是转义符
					
					return pictureUUIDName;
					
				} catch (IOException e) {
					
					System.out.println("图片文件上传出错");
					e.printStackTrace();
					
					return null;
					
				}
	}
	
	public String listFileUploadHandler(List<MultipartFile> files){
		
		//1.循环上传文件   2.拼接文件路径信息
		String pictureTempNameList="";
		
		//用户上传多个文件时
		if(files.size()>1){
			
			for(MultipartFile file:files){
	
				pictureTempNameList+=this.singleFileUploadHandler(file)+";";
				
			}
			
			
		}else{
				
			//用户上传单个文件时	
			MultipartFile file=files.get(0);
			pictureTempNameList=this.singleFileUploadHandler(file);
						
		}
		
		//去除最后一个分号（；）
		System.out.println(pictureTempNameList.endsWith(";"));
		
		if(pictureTempNameList.endsWith(";")){
			
			pictureTempNameList=pictureTempNameList.substring(0,pictureTempNameList.length()-1);
		
		}
		System.out.println(pictureTempNameList);
		
		return pictureTempNameList;
	}
	
	
	
}
